package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.util.HashMap;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.TierConfigDTO;

public class CashToPoints extends ServletPojoAdapter {
	private static final long serialVersionUID = 3205628753230344740L;

	protected Map<String,Object> getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan = params.get("pan")!= null ? params.get("pan")[0]:null;
		final String cashVal = params.get("cashval")!=null ? params.get("cashval")[0]:null;
		
		//Default response
		final Map<String,Object> ret = new HashMap<String, Object>();
		ret.put("status", "Card Not Found");		
		ret.put("points", "0");
		final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);
		if (accountDetail!=null) {
			final TierConfigDTO tierDetail = accountDetail.getTierConfig();
			final Double cpp = new Double(tierDetail.getCostPerPoint());
			try {
				final Double cash = new Double(cashVal);
				final Double pointsVal = cash / cpp;
				System.out.println("Points Value:- >"+pointsVal);
				//pointsVal.intValue();
				//ret.put("points", pointsVal);
				ret.put("points", pointsVal.intValue());
				ret.put("status", "Success");
			} catch (NumberFormatException ex) {
				ret.put("status", "Invalid Cash Amount");
			}
		}
		return ret;
	}

}
